﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;

namespace BBCore.Infrastructure.Helps
{
    /// <summary>
    /// 获取当前项目框架版本号
    /// </summary>
    public static class GetTargetFrameworkVersion
    {
        /// <summary>
        /// 通过反射获取入口程序集的 TargetFrameworkAttribute，并解析其版本信息
        /// </summary>
        /// <returns>例如:"net8.0"</returns>
        public static string GetTargetFrameworkMoniker()
        {
            var entryAssembly = Assembly.GetEntryAssembly();
            if (entryAssembly == null) return "Unknown";

            var attribute = entryAssembly.GetCustomAttribute<TargetFrameworkAttribute>();
            if (attribute == null) return "Unknown";

            // 示例 FrameworkName: ".NETCoreApp,Version=v8.0"
            string versionPart = attribute.FrameworkName.Split(',')[1].Trim();
            string version = versionPart.Split('=')[1].Trim().Substring(1); // 提取 "8.0"

            return $"net{version}"; // 返回 "net8.0"
        }
    }
}
